import { Injectable, Inject } from '@nestjs/common';

import { DBCONSTANTS } from '@/database/db.constants';

import { User } from './interface/user.interface';
import { Model } from 'mongoose';

@Injectable()
export class UserService {
  @Inject(DBCONSTANTS.userModel)
  private readonly userModel: Model<User>;

  /**
   * 创建用户
   * @param user 创建用户
   */
  async createUser(user: User): Promise<User> {
    const newUser = new this.userModel(user);
    return await newUser.save();
  }
  /**
   * 根据用户名查找用户
   * @param username 用户名
   */
  async findUserByUserName(username: string) {
    return await this.userModel.findOne({ username });
  }
  /**
   * 根据用户ID查找用户
   * @param userId 用户ID
   */
  async findUserByUserId(userId: string) {
    return await this.userModel.findOne({ _id: userId });
  }

  /**
   * 根据用户id去更新用户信息
   * @param id
   * @param user
   */
  async updateUserById(id: string, user: User): Promise<User> {
    return await this.userModel.findByIdAndUpdate(id, user, { new: true });
  }
}
